package grupb.upf.imageProcessing;


import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.JAI;
import javax.media.jai.LookupTableJAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.RenderedOp;

public final class Contrast {

    public RenderedOp execute(final PlanarImage img, final int contrasting) {
        final ParameterBlock mPB = new ParameterBlock();
        mPB.addSource(img);
        final byte[] lutData = new byte[256];
        for (int j = 0; j < 256; j++) {
            lutData[j] = clampByte(j + contrasting);
        }
        final LookupTableJAI mLookup = new LookupTableJAI(lutData);
        mPB.add(mLookup);
        
        final RenderedOp mRenOp = JAI.create("lookup", mPB, null);
        return mRenOp;

        
    }
    private byte clampByte(int i){
        if(i > 255)
            return -1;
        if(i < 0)
            return 0;
        else
            return (byte)i;
    }
}
